package com.erpanderp.wam.adm.view;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import org.primefaces.component.datatable.DataTable;

import com.erpanderp.wam.adm.model.entity.Module;
import com.erpanderp.wam.adm.model.entity.Resource;
import com.erpanderp.wam.adm.model.entity.Service;
import com.erpanderp.wam.adm.model.entity.ServiceGroup;
import com.erpanderp.wam.adm.model.service.ModuleDAO;
import com.erpanderp.wam.adm.model.service.ServiceDAO;
import com.erpanderp.wam.adm.model.service.ServiceGroupDAO;
import com.erpanderp.wam.util.CommonLookup;
import com.erpanderp.wam.util.ValidationUtil;

@ManagedBean
@ViewScoped
public class CreateServiceGroup implements Serializable {
	
	private static final long serialVersionUID = 1L;	
	private String assignServiceGroupButtonLabel = CommonLookup.ASSIGN_SERVICE_LABEL;
	ModuleDAO moduleDAO = new ModuleDAO();
	ServiceGroupDAO serviceGroupDAO = new ServiceGroupDAO();
	ServiceDAO serviceDAO = new ServiceDAO();
	private Map<String, Long> modulesMap = new HashMap<String, Long>();
	private List<Service> services = new ArrayList<Service>();	
	private Service[] selectedServices;
	private List<Service> assignedServices = new ArrayList<Service>();
	private Set<Service> assignedServicesSet = new HashSet<Service>();
	private Service selectedService;
	private DataTable assignedServicesTable;
	private boolean assignServicesToServiceGroup;
	
	private Long selectedModuleId;
	private ServiceGroup serviceGroup = new ServiceGroup();
	private boolean FORM_VALID;
	
	public CreateServiceGroup() {
		List<Module> modules = moduleDAO.getAllModules();
		for (Module module : modules) {
			modulesMap.put(module.getModuleName(), module.getModuleId());
		}		
	}
	
	public void saveServiceGroup(ActionEvent e){
		System.out.println("INSIDE SAVE SERVICE GROUP()");
		validateForm();
		if(FORM_VALID){
			try{
				serviceGroup.setCreateDate(new Date());
				serviceGroup.setCreatedBy( "DEVELOPER");
				serviceGroup.setStatus("ACTIVE");
				serviceGroup.setModule(moduleDAO.getModuleById(selectedModuleId));
				serviceGroup.getServices().addAll(assignedServices);
				serviceGroupDAO.createServiceGroup(serviceGroup);
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, serviceGroup.getServicegroupName() + " Service Group created successfully.", ""));
				serviceGroup = new  ServiceGroup();	
			}catch (Exception ex) {
				FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, "Failed to create Service Group.", ex.getMessage()));
			}
			
		}else{
			
		}
		System.out.println("IN SAVE MODULE");
	}
	
   public void toggleAssignPanelDisplay(ActionEvent e){
		
		System.out.println("In toggleAssignPanelDisplay");
	
		if(assignServiceGroupButtonLabel.equals(CommonLookup.ASSIGN_SERVICE_LABEL)){
			setAssignServiceGroupButtonLabel(CommonLookup.DO_NOT_ASSIGN_SERVICE_LABEL);
			setAssignServicesToServiceGroup(true);
			System.out.println(selectedModuleId);
			setServices(serviceDAO.getServicesByModuleId(selectedModuleId));			
		}else{
			setAssignServiceGroupButtonLabel(CommonLookup.ASSIGN_SERVICE_LABEL);
			setAssignServicesToServiceGroup(false);
		}		
   }
   
   public void assignService(ActionEvent e){
		assignedServices.addAll( new ArrayList<Service>(Arrays.asList(selectedServices)));
	
	}
	
	
	public void removeService(ActionEvent e){
		Service selectedService = (Service) assignedServicesTable.getRowData();
		assignedServicesSet.remove(selectedService);
		assignedServices.remove(selectedService);	
	}
	
	public void assignServiceAction(ActionEvent e) {
		assignedServicesSet.addAll( new ArrayList<Service>(Arrays.asList(selectedServices)));
		assignedServices.clear();
		assignedServices.addAll(assignedServicesSet);	
	}
	
	public String assignSevices() {
		System.out.println("In Assign Resources");
		return "Assigned";
	}


	private void validateForm(){
		FORM_VALID = true;
		
		if(ValidationUtil.isEmpty(serviceGroup.getServicegroupName(), "Service Group Name", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		if(ValidationUtil.isEmpty(serviceGroup.getDisplayName(), "Display Order", null, FacesContext.getCurrentInstance()) != null){
			FORM_VALID = false;
		}
		
	}
	
	
	
	public Map<String, Long> getModulesMap() {
		return modulesMap;
	}
	public void setModulesMap(Map<String, Long> modulesMap) {
		this.modulesMap = modulesMap;
	}	
	
	public Long getSelectedModuleId() {
		return selectedModuleId;
	}
	public void setSelectedModuleId(Long selectedModuleId) {
		this.selectedModuleId = selectedModuleId;
	}
	
	public boolean isFORM_VALID() {
		return FORM_VALID;
	}
	public void setFORM_VALID(boolean fORM_VALID) {
		FORM_VALID = fORM_VALID;
	}
	
	public ServiceGroup getServiceGroup() {
		return serviceGroup;
	}
	public void setServiceGroup(ServiceGroup serviceGroup) {
		this.serviceGroup = serviceGroup;
	}

	public String getAssignServiceGroupButtonLabel() {
		return assignServiceGroupButtonLabel;
	}

	public void setAssignServiceGroupButtonLabel(
			String assignServiceGroupButtonLabel) {
		this.assignServiceGroupButtonLabel = assignServiceGroupButtonLabel;
	}
	public boolean isAssignServicesToServiceGroup() {
		return assignServicesToServiceGroup;
	}

	public void setAssignServicesToServiceGroup(boolean assignServicesToServiceGroup) {
		this.assignServicesToServiceGroup = assignServicesToServiceGroup;
	}
	public Service[] getSelectedServices() {
		return selectedServices;
	}

	public void setSelectedServices(Service[] selectedServices) {
		this.selectedServices = selectedServices;
	}

	public List<Service> getAssignedServices() {
		return assignedServices;
	}

	public void setAssignedServices(List<Service> assignedServices) {
		this.assignedServices = assignedServices;
	}

	public Service getSelectedService() {
		return selectedService;
	}

	public void setSelectedService(Service selectedService) {
		this.selectedService = selectedService;
	}

	public DataTable getAssignedServicesTable() {
		return assignedServicesTable;
	}

	public void setAssignedServicesTable(DataTable assignedServicesTable) {
		this.assignedServicesTable = assignedServicesTable;
	}

	public List<Service> getServices() {
		return services;
	}

	public void setServices(List<Service> services) {
		this.services = services;
	}

	public Set<Service> getAssignedServicesSet() {
		return assignedServicesSet;
	}

	public void setAssignedServicesSet(Set<Service> assignedServicesSet) {
		this.assignedServicesSet = assignedServicesSet;
	}



}
